//
//  PersonalDataViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/20.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "PersonalDataViewController.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#import "ReceiptAddressViewController.h"
#import "PopViewController.h"
@interface PersonalDataViewController ()<UITableViewDelegate,UITableViewDataSource,UIPopoverPresentationControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    PopViewController *_popVC;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSourceArr;


@end

@implementation PersonalDataViewController
- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithObjects:@"会员名",@"昵称",@"性别",@"我的二维码名片",@"我的收获地址", nil];
    }
    return _dataSourceArr;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ChangeHeadImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChangeHeadImageTableViewCell"];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.view addSubview:self.tableView];
    //导航栏添加控件
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"← 个人资料" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
        self.navigationItem.leftBarButtonItem = backItem;

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"message"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(popViewAction)];
    self.navigationItem.rightBarButtonItem = rightItem;

}
- (void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


//点击导航栏右侧按钮弹出悬浮框
-(void)popViewAction{

    _popVC = [[PopViewController alloc] init];

    _popVC.modalPresentationStyle = UIModalPresentationPopover;

    //设置依附的按钮
    _popVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;

    //可以指示小箭头颜色
    _popVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];

    //content尺寸
    _popVC.preferredContentSize = CGSizeMake(400, 400);

    //pop方向
    _popVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    //delegate
    _popVC.popoverPresentationController.delegate = self;
    
    _popVC.viewController = self;
    
    [self presentViewController:_popVC animated:YES completion:nil];


}
//代理方法 ,点击即可dismiss掉每次init产生的PopViewController
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}
#pragma mark ----- UITableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       return self.dataSourceArr.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ChangeHeadImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeHeadImageTableViewCell" forIndexPath:indexPath];
        self.headImage = cell.headImage;
        return cell;

    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text = self.dataSourceArr [indexPath.row - 1];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row == 0) {
            return 100;
        }else{
            return 50;

    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            //初始化弹出框
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

            //按钮：从相册选择
            [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"%@",[NSThread currentThread]);
                //初始化UIImagePickerController
                UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
                //获取方式
                pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //允许编辑
                pickerController.allowsEditing = YES;
                //设置代理
                pickerController.delegate = self;
                //页面跳转
                [self presentViewController:pickerController animated:YES completion:nil];
            }]];

            //按钮：拍照
            [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //初始化UIImagePickerController
                UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
                pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                pickerController.allowsEditing = YES;
                pickerController.delegate = self;
                [self presentViewController:pickerController animated:YES completion:nil];
            }]];

            //按钮：取消
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            
            [self presentViewController:alert animated:YES completion:nil];

        }
            break;
        case 5:{
            ReceiptAddressViewController *addressVC = [[ReceiptAddressViewController alloc]init];
            [self.navigationController pushViewController:addressVC animated:YES];
        }
            break;

        default:
            break;
    }

}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissViewControllerAnimated:YES completion:^{

    }];

}
//pickerImage完成后的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    //    //通过UIImagePickerControllerMediaType判断返回的是照片还是视频
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //     //如果返回的type等于kUTTypeImage，代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册

    if ([type isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *theImage = nil;
        //判断照片是否允许修改
        if ([picker allowsEditing]) {
            //获取用户编辑之后的图像
            theImage = [info objectForKey:UIImagePickerControllerEditedImage];
            self.headImage.image = theImage;
        }else{
            //照片的元数据参数
            theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
            self.headImage.image = theImage;
        }


        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
    

}/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
